﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaneController : MonoBehaviour
{
    public Vector2 Velocity;
    [SerializeField, Range(0.1f, 1.5f), Tooltip("纸飞机的质量很小，所以风力很小")]
    float m_WindForce = 0.8f;
    [SerializeField, Range(0.1f, 0.8f), Tooltip("向左吹的风的风力系数")]
    float m_xReverseK = 0.5f;
    Rigidbody2D planeRigidbody2D;
    Vector2 windForce = Vector2.zero;
    private void Awake()
    {
        planeRigidbody2D = GetComponent<Rigidbody2D>();
    }
    private void Update()
    {
        windForce.x = Input.GetAxis(Constants.Text_Input_Horizontal);
        windForce.y = Input.GetAxis(Constants.Text_Input_Vertical);
        windForce *= m_WindForce;
        //往回吹的风风力会变小
        if (windForce.x < 0f)
            windForce.x *= m_xReverseK;
        Velocity = planeRigidbody2D.velocity;
        if (Velocity.x < 0f) //不允许往回吹
        {
            windForce.x = 0f;
            Velocity.x = 0f;
            planeRigidbody2D.velocity = Velocity;
        }
        // 到达上边界就不可以往上吹了
        if (transform.position.y >= CameraController.Instance.MoveRangeYTop)
        {
            windForce.y = 0f;
        }
    }
    void FixedUpdate()
    {
        planeRigidbody2D.AddForce(windForce, ForceMode2D.Force);
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (GameManager.Instance.GameState != Constants.GameState_Win)
        {
            GameManager.Instance.GameState = Constants.GameState_Lose;
            enabled = false;
            GetComponent<Animator>().enabled = false;
            GameManager.Instance.ShowLoseMessageBox();
        }
    }
}
